package com.shoppingmall.controller.backstage;

import com.shoppingmall.common.ServletResponse;
import com.shoppingmall.pojo.User;
import com.shoppingmall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * Created by
 * 处理管理员登录的
 * @author touni
 * @date 2019/3/19
 * @time 15:05
 */
@RestController
@RequestMapping("/manager/user")
public class ManagerUserController {

    @Autowired
    private UserService userService;

    /**
     * 存储用户的session的名称
     */
    @Value("${USER_SESSION}")
    private String userSessoin;

    /**
     * 管理员登录
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ServletResponse managerLogin(String username, String password, HttpSession session){
        //检查用户名是否存在
        ServletResponse<Integer> checkUsername = userService.checkValue(username, 1);
        if (1 != checkUsername.getData()){
            return ServletResponse.createByErrorMessage("用户名不存在，请重新输入");
        }

        ServletResponse<User> resultResponse = userService.userLogin(username, password);
        User userFromQuery = resultResponse.getData();
        if (null == userFromQuery){

            return ServletResponse.createByErrorMessage("用户名或者密码有误，请重新输入");
        }

        if (0 != userFromQuery.getRole().intValue()){
            return ServletResponse.createByErrorMessage("当前用户不是管理员，登录失败");
        }
        session.setAttribute(userSessoin,userFromQuery);
        return ServletResponse.createBySuccess(userFromQuery);


    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public ServletResponse listALlUser(@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                       @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum){
        // todo 完成所有用户的查询
        return ServletResponse.createBySuccess("未进行查询，待完善！！！");





    }








}
